[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
fopen() Open a File
#include <stdio.h>
FILE *fopen(pathname,type);
const char *pathname; Path name of file
const char *type; Type of access permitted
fopen() opens the file specified by 'pathname'. 'type' is a
character string that specifies the type of access requested for the
file:
"r" Open for reading only. The file must exist.
"w" Create a new file for writing, or open an existing file
for writing. Use with care: If the file already exists,
its contents will be destroyed.
"a" Append. Open for writing at the end-of-file, or create
for writing if the file doesn't exist. Existing data
cannot be overwritten in this mode.
"r+" Update. Open an existing file for reading and writing;
the file must exist.
"w+" Open an empty file for reading and writing. Use with
care: if the file exists, its contents will be destroyed.
"a+" Open for reading and appending; create the file if it
doesn't exist. All write operations take place at the
end of the file; existing data cannot be overwritten.
The following characters may be appended to the above 'type' to
specify the translation mode for the new lines:
t Open in text (translated) mode; The CR-LF pair is
translated to a single LF on input; LF is translated to
CR-LF on output.
b Open in binary (untranslated) mode. The above
translation is suppressed.
If 't' or 'b' is not specified, the translation mode is defined by
the default mode variable '_fmode'. '_fmode' can be set to O_BINARY,
binary mode, or O_TEXT, text mode. These constants are defined in
<fcntl.h>.
Returns: A pointer to the open file. NULL is returned on
error.
Notes: Both reading and writing are allowed when using "r+",
"w+", or "a+".
Before switching between reading and writing, there
must be an intervening fseek() or rewind(). Only when
a read() returns EOF can it be followed immediately
by a write()
-------------------------------- Example ---------------------------------
The following statements open and close a file.
#include <stdio.h>
FILE *in;
main()
{
if ((in = fopen("new.dat","w+")) != NULL) {
.
.
.
fclose(in);
}
}
See Also:
fdopen()
freopen()
open()
fclose()
fcloseall()
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson